package com.mc.moring.base;

import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class BaseFragment extends Fragment {

    public abstract void initView();
    public abstract void initData();

    public void isShowView(String value, View view, TextView textView, String text) {
        view.setTag(value);
        if (TextUtils.equals(value, "1")) {
            view.setVisibility(View.VISIBLE);
            String tip_value =
//                    "<font color='#C84958'>* </font>"+
                            text;
            textView.setText(Html.fromHtml(tip_value));
        } else if (TextUtils.equals(value, "2")) {
            view.setVisibility(View.VISIBLE);
            textView.setText(text ); //+ " (opcional)"
            if (text.contains("Apellido materno")) {
                textView.setText("Apellido materno (opcional)");
            }
        } else {
            view.setVisibility(View.GONE);
        }
    }

    public void isOhterShowView(String value, View view, TextView textView, String text) {
        view.setTag(value);
        if (TextUtils.equals(value, "1")) {
            view.setVisibility(View.VISIBLE);
            textView.setText(text);
        } else if (TextUtils.equals(value, "2")) {
            view.setVisibility(View.VISIBLE);
            text = text.toString() + " (opcional)";
            textView.setText(text); //+ " (opcional)"
        } else {
            view.setVisibility(View.GONE);
        }
    }

    public static boolean hasChinese(String input) {
        String regex = "[\u4e00-\u9fa5]";
        return Pattern.matches(".*" + regex + ".*", input);
    }


    public boolean verificationIDNumber(int minLen, int maxLen, String idNumber){
        if(idNumber.length()>maxLen || idNumber.length()<minLen){
            return false;
        }
//        Pattern pattern = Pattern.compile("[a-zA-Z0-9]+");
        Pattern pattern = Pattern.compile("[0-9]+");
        Matcher matcher = pattern.matcher(idNumber);
        return matcher.matches();

    }

    public boolean isNumber(String idNumber) {
        Pattern pattern = Pattern.compile("[0-9]+");
        Matcher matcher = pattern.matcher(idNumber);
        return matcher.matches();
    }

    public boolean verificationBankAccountBase(int minLen, int maxLen, String idNumber) {
        if (idNumber.length() < minLen || idNumber.length() > maxLen) {
            return false;
        }
        return idNumber.matches("\\d{" + minLen + "," + maxLen + "}");
    }


    public boolean verificationBankAccount(int minLen, int maxLen, String idNumber) {
        if (idNumber.length() < minLen || idNumber.length() > maxLen) {
            return false;
        }

        String prefix = idNumber.substring(0, idNumber.length() - 1);
        char lastChar = idNumber.charAt(idNumber.length() - 1);

        boolean isPrefixDigits = prefix.matches("\\d+");
        boolean isLastCharK = lastChar== 'K';
        boolean isPrefixElevenModAlgorithm = false;
        boolean isAllElevenModAlgorithm = false;
        if (isPrefixDigits) {
            isPrefixElevenModAlgorithm = isElevenModAlgorithm(prefix);
        }

        if (isLastCharK) {
            return isPrefixDigits && isLastCharK && isPrefixElevenModAlgorithm;
        } else {
            if (idNumber.matches("\\d{" + minLen + "," + maxLen + "}")) {
                isAllElevenModAlgorithm = isElevenModAlgorithm(idNumber);
            }
            return isAllElevenModAlgorithm;
        }
    }

    protected static boolean isElevenModAlgorithm(String str) {
        // mod 11
        int sum = 0;
        for (int i = 0; i < str.length(); i++) {
            int digit = Character.getNumericValue(str.charAt(i));
            sum += (i % 2 == 0) ? digit : -digit;
        }
        return (sum % 11) == 0;
    }


    public boolean isDateCorrect(String data){
        if(data.length()==10 && TextUtils.equals(data,"-")){
            String[] dataArry = data.split("-");
            Pattern p = Pattern.compile("[1-9]\\d*");
            String str = dataArry[0];
            Matcher m = p.matcher(str);
            return m.matches();
        }
        return false;
    }

    public static boolean isValidBirthday(String birthdayString) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");

        try {
            Date birthday = dateFormat.parse(birthdayString);

            Calendar currentDate = Calendar.getInstance();
            currentDate.setTime(new Date());

            Calendar birthdayDate = Calendar.getInstance();
            birthdayDate.setTime(birthday);
            int age = currentDate.get(Calendar.YEAR) - birthdayDate.get(Calendar.YEAR);


            return age >= 18 && age <= 60;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean emailFormat(String data){
        List<String> stringList = new ArrayList<>();
        stringList.add("@gmail.com");
        stringList.add("@yahoo.com");
        stringList.add("@ymail.com");
        stringList.add("@live.com");
        stringList.add("@hotmail.com");
        stringList.add("@aol.com");
        stringList.add("@webmail.com");
        stringList.add("@vodamail.com");
        stringList.add("@webmail.co.za");
        stringList.add("@vodamail.co.za");
        stringList.add("@iafrica.com");
        for (int i = 0; i <stringList.size() ; i++) {
            if(data.contains(stringList.get(i))){
                return true;
            }
        }
        return false;
    }

    // 邮箱格式校验方法
    public static boolean isValidEmail(String email) {
//        String regex = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
        String regex = "^([a-zA-Z0-9_\\-.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

    public void linearLayoutOffsetY(NestedScrollView linearLayout, View targetChild){
        int scrollToPositionY = targetChild.getTop();
        linearLayout.smoothScrollTo(0, scrollToPositionY);
    }

}
